/*
 * persona.cpp
 *
 */

#include <exception>
#include "Argv.h"
#include "StringUtils.h"
#include "logger/logger.h"
#include "API/Puerta.h"
#include "API/SalaBus.h"
#include "API/Bus.h"

int main(int argc, char **argv)
{
    Argv arguments;
    PersonaId personaId;
    PuertaId puertaId;
    SalaId salaId;

    arguments.setArgv(argv);

    if(argc > 1)
    	personaId = StringUtils::StringToNumber<int>(arguments.getArg(1));
    else
    	personaId = 2;

    if(argc > 2)
    	puertaId = StringUtils::StringToNumber<int>(arguments.getArg(2));
    else
    	puertaId = 1;

    //TODO improve?
    salaId = puertaId*2-1;

    std::stringstream ss;
    ss<<"Persona "<< personaId;

	CLogger::create(ss.str());
	CLogger::setLevel(LogLevelEnum::DEBUG);

	try
	{
		API::Puerta puerta(API::Puerta::CLIENT);
		API::SalaBus sala(API::SalaBus::CLIENT);

		API::Persona persona;
		BusId busId;

		int err;

		persona.m_id = personaId;

		//TODO ver si entrar parque devuelve id sala
		if(!puerta.entrarParque(puertaId,persona,err))
			return 0;

		busId = sala.esperarBus(salaId, persona,err);

		API::Bus bus(busId);

		bus.suboAlBus(persona,err);
	}
	catch (std::exception &e)
	{
		CLogger::log(e.what(),LogLevelEnum::FATAL);
	}
	catch (std::exception *e)
	{
		CLogger::log(e->what(),LogLevelEnum::FATAL);
	}
	return 0;
}
